package com.mangguo.tingjian.activity.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mangguo.tingjian.activity.dto.TodayActivityRankResult;
import com.mangguo.tingjian.activity.dto.TodayActivityResult;
import com.mangguo.tingjian.activity.dto.TodayChannelRankResult;
import com.mangguo.tingjian.activity.dto.TrendResult;
import com.mangguo.tingjian.activity.entity.DwChannelOpenApp;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mangguo.tingjian.activity.req.ActivityPageReq;
import com.mangguo.tingjian.statistics.res.RetentionRateLineRes;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author auto
 * @since 2019-11-25
 */
@Mapper
public interface DwChannelOpenAppMapper extends BaseMapper<DwChannelOpenApp> {

    /**
     * 活动
     * @param mDate
     * @param subChannelId
     * @param processType
     * @return
     */
    @Select("select count(1) from (select userId from dw_channel_open_app where mDate=#{mDate} and subchannelId=#{subChannelId} and processType=#{processType}) as a")
    int selectActivityCountByDateAndType(@Param("mDate") int mDate, @Param("subChannelId") String subChannelId, @Param("processType") int processType);

    /**
     * 渠道
     * @param mDate
     * @param channelId
     * @param processType
     * @return
     */
    @Select("select count(1) from (select userId from dw_channel_open_app where mDate=#{mDate} and channelId=#{channelId} and processType=#{processType}) as a")
    int selectChannelCountByDateAndType(@Param("mDate") int mDate, @Param("channelId") String channelId, @Param("processType") int processType);


    /**
     * 活动
     * @param subChannelId
     * @param processType
     * @return
     */
    @Select("select count(1) from (select userId from dw_channel_open_app where subchannelId=#{subChannelId} and processType=#{processType}) as a ")
    int selectActivityCountByTypeAndChannel(@Param("subChannelId") String subChannelId, @Param("processType") int processType);


    /**
     * 渠道
     * @param channelId
     * @param processType
     * @return
     */
    @Select("select count(1) from (select userId from dw_channel_open_app where channelId=#{channelId} and processType=#{processType}) as a ")
    int selectChannelCountByTypeAndChannel(@Param("channelId") String channelId, @Param("processType") int processType);

    /**
     * 按小时查询激活趋势数据  活动
     * @param subChannelId
     * @param processType
     * @param mHourStart
     * @param mHourEnd
     * @return
     */
    List<TrendResult> selectActivityCountByChannelAndTypeGroupByHour(@Param("subChannelId") String subChannelId, @Param("processType") int processType, @Param("mHourStart")Integer mHourStart, @Param("mHourEnd") Integer mHourEnd);


    /**
     * 按小时查询激活趋势数据  渠道
     * @param channelId
     * @param processType
     * @param mHourStart
     * @param mHourEnd
     * @return
     */
    List<TrendResult> selectChannelCountByChannelAndTypeGroupByHour(@Param("channelId") String channelId, @Param("processType") int processType, @Param("mHourStart")Integer mHourStart, @Param("mHourEnd") Integer mHourEnd);

    /**
     * 活动
     * @param mDate
     * @param processType
     * @return
     */
    @Select("select subchannelId, count(userId) as countNum from dw_channel_open_app where mDate=#{mDate} and processType=#{processType} group by subchannelId order by countNum desc limit 10")
    List<TodayActivityRankResult> selectActivityTodayActivityRank(@Param("mDate") int mDate, @Param("processType") int processType);

    /**
     * 渠道
     * @param mDate
     * @param processType
     * @return
     */
    @Select("select subchannelId, count(userId) as countNum from dw_channel_open_app where mDate=#{mDate} and processType=#{processType} group by channelId order by countNum desc limit 10")
    List<TodayChannelRankResult> selectChannelTodayActivityRank(@Param("mDate") int mDate, @Param("processType") int processType);


    /**
     * 活动
     * @param page
     * @param req
     * @return
     */
    @Select("select subChannelId, count(userId) as countNum ")
    IPage<TodayActivityResult> selectActivityPageResult(IPage<TodayActivityResult> page, @Param("req") ActivityPageReq req );

}
